package org.example.controller;

import com.github.yitter.idgen.YitIdHelper;
import org.example.model.message.AccountChangeMessage;
import org.example.service.AccountInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author shenguangyang
 */
@RestController
public class AccountInfoController {
    private static final Logger log = LoggerFactory.getLogger(AccountInfoController.class);

    @Resource
    private AccountInfoService accountInfoService;

    /**
     * <a href="http://127.0.0.1:9090/transfer?accountNo=2&amount=10">给李四转账</a>
     * @param accountNo
     * @param amount
     * @return
     */
    @GetMapping(value = "/transfer")
    public String transfer(@RequestParam("accountNo")String accountNo, @RequestParam("amount") BigDecimal amount){
        String txNo = YitIdHelper.nextId() + "";
        AccountChangeMessage accountChangeMessage = AccountChangeMessage.builder()
                .amount(amount)
                .accountNo(accountNo)
                .build();
        accountChangeMessage.setMsgId(txNo);
        accountInfoService.sendUpdateAccountBalance(accountChangeMessage);
        return "转账成功";
    }
}
